package com.wyt.controller;

import com.wyt.controller.util.Data;
import com.wyt.pojo.User;
import com.wyt.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@Controller
public class IndexController {
    @Autowired
    private UserService userService;

    /**
     * 首页
     * @return
     */
    @GetMapping("/")
    public String login() {
        return "bookPage/index.html";
    }

    /**
     * 登录验证
     * @param ID
     * @param password
     * @return
     */
    @PostMapping("/index/passwordValidate")
    @ResponseBody
    public Data passwordValidate(String ID, String password, HttpSession session) {
        User user = userService.getUser(ID, password);
        if (user != null) {
            Integer limit = user.getLimit();
            if (limit==1){
                return new Data("1", "登陆成功", "admin");
            }else {
                session.setAttribute("stu.ID",user.getId());
                return new Data("1", "登陆成功", "stu/index");
            }
        } else {
            return new Data("0", "用户名或密码错误，请重新登录", "null");
        }
    }



}
